/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tmro.mobile.txtapark.forms;

import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.StringItem;
import net.tmro.mobile.txtapark.HelloMIDlet;

/**
 *
 * @author tmro
 */
public class MenuForm extends Form implements ItemCommandListener {

    private StringItem payStringItem;
    private StringItem historyStringItem;
    private final HelloMIDlet midlet;

    public MenuForm(HelloMIDlet midlet) {
        super("Menu");
        this.midlet = midlet;
    }

    public void createUI() {
        Image image = null;
        try {
            image = Image.createImage("txt-a-park.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.append(new ImageItem("", image, Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER, "TXT-a-Park"));
        payStringItem = new StringItem("", "Pay for parking", StringItem.BUTTON);
        payStringItem.setDefaultCommand(new Command("Pay", Command.OK, 0));
        payStringItem.setItemCommandListener(this);
        payStringItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
        this.append(payStringItem);

        final Spacer spacer = new Spacer(10, 10);
        spacer.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
        this.append(spacer);


        historyStringItem = new StringItem("", "Parking history", StringItem.BUTTON);
        historyStringItem.setDefaultCommand(new Command("History", Command.OK, 0));
        historyStringItem.setItemCommandListener(this);
        historyStringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
        this.append(historyStringItem);

        this.append(new Spacer(100, 50));
        this.append("There is no association whatsoever between the developer and the Text-a-Park trademark owners. This application was built to add more value to the parking experience by allowing you to track when you parked. Please email office@tmro.net if you have any comments or queries.");

    }

    public void commandAction(Command c, Item item) {
        System.out.println("Handling menu click");
        if (item == payStringItem) {
            System.out.println("Loading the pay form");
            PayForm payForm = new PayForm(midlet);
            payForm.setCommandListener(midlet);
            payForm.addCommand(midlet.menuCommand);
            payForm.createUI();

            Display.getDisplay(midlet).setCurrent(payForm);

        } else if (item == historyStringItem) {
            System.out.println("Loading the history form");
            HistoryForm historyForm = new HistoryForm(midlet);
            historyForm.setCommandListener(midlet);
            historyForm.addCommand(midlet.menuCommand);
            historyForm.createUI();

            Display.getDisplay(midlet).setCurrent(historyForm);
        }
    }
}
